package cn.trigram.mgb.simple;

import java.util.List;
import java.util.Set;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.TopLevelClass;


public class Mybatis3SimpleCommentPlugin extends PluginAdapter {

  @Override
  public boolean validate(List<String> warnings) {

    return true;
  }

  @Override
  public boolean modelFieldGenerated(
      Field field, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn,
      IntrospectedTable introspectedTable, ModelClassType modelClassType
  ) {

    Set<FullyQualifiedJavaType> imports = topLevelClass.getImportedTypes();
    imports.add(new FullyQualifiedJavaType("javax.annotation.Generated"));
    String generatedComment = String.format(
        "@Generated(comments = \"Source field: %s.%s\", value = {})",
        introspectedTable.getFullyQualifiedTable().toString(), introspectedColumn.getActualColumnName()
    );
    field.addAnnotation(generatedComment);
    return true;
  }

}
